package com.neusoft.a132wxyorderingdish.fragments;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.neusoft.a132wxyorderingdish.R;

public class CollectFragment extends Fragment {


    private RadioGroup rg;
    private RadioButton collect_food,collect_shop;
    private CollectShopFragment collectShopFragment;
    private CollectFoodFragment collectFoodFragment;
    private View view=null;
    protected FragmentManager childManager;
    protected FragmentTransaction transaction;

    public CollectFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        view= inflater.inflate(R.layout.fragment_collect, container, false);
        initViews();
        initEvents();
        return view;
    }
    public void onResume(){
        super.onResume();
        if(collectShopFragment==null){
            collectShopFragment=new CollectShopFragment();
            addFragment(collectShopFragment);
        }
        else{replaceFragment(collectShopFragment);}
    }
    private void initViews(){
        childManager=getChildFragmentManager();
        collect_food=view.findViewById(R.id.radio_collect_food);
        collect_shop=view.findViewById(R.id.radio_collect_shop);
        rg=view.findViewById(R.id.radioGroup_collect);
        collect_shop.setSelected(true);



    }
    private void initEvents(){
        rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int checkedId) {
                switch (checkedId){
                    case R.id.radio_collect_shop:
                        if(collectShopFragment==null){
                            collectShopFragment=new CollectShopFragment();
                        }
                        replaceFragment(collectShopFragment);
                        collect_shop.setSelected(true);
                        collect_food.setSelected(false);
                        break;
                    case R.id.radio_collect_food:
                        if(collectFoodFragment==null){
                            collectFoodFragment=new CollectFoodFragment();
                        }
                        replaceFragment(collectFoodFragment);
                        collect_food.setSelected(true);
                        collect_shop.setSelected(false);
                        break;
                }
            }
        });
    }
    protected void addFragment(Fragment fragment){
        transaction=childManager.beginTransaction();
        transaction.add(R.id.fragment_container,fragment);
        transaction.commit();
    }

    protected void replaceFragment(Fragment fragment){
        transaction=childManager.beginTransaction();
        transaction.replace(R.id.fragment_container,fragment);
        transaction.commit();
    }
}

